import java.awt.*;
import javax.swing.*;

public class AnimatedHeart extends JPanel {
    private int x = 100; // 初始位置
    private int y = 100;
    private int dx = 2; // x方向上的速度
    private int dy = 2; // y方向上的速度

    public AnimatedHeart() {
        new Thread(() -> {
            while (true) {
                repaint();
                try {
                    Thread.sleep(50); // 控制动画的速度
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制心形符号
        g.drawString("❤", x, y);
        // 更新位置
        x += dx;
        y += dy;
        // 边界检测
        if (x > getWidth()) {
            x = -10;
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Animated Heart");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new AnimatedHeart());
        frame.setSize(400, 400);
        frame.setVisible(true);
    }
}